var app = getApp();
var util = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choosemodel: ["固定价", '时价'],
    selectdanwei: false,  //选择单位弹窗
    showModalStatus: false,  //底部分裂弹窗
    selectdingjia: true,  //定价模式下拉框
    shopimg:'',    //上传图片地址
    shopname:'',    //商品名称
    shopordername: '',   //商品别名
    shopunit:'',  //商品单位
    one_class_quename:'',  //一级分类名称
    two_class_quename: '',  //二级分类名称
    dingjia:'时价',  //定价模式名称
    shopmoney:'', //商品单价
    price_type:2,  //定价模式索引值
    value: [0, 0],  //底部弹窗初始值
    xuni_status:'',
    specs_deatail:''
  },
  // 上传图片
  changeimg:function(){
    var that = this;
    wx.navigateTo({
      url: '../../../cropper/cropper?imgx=3'
    })
  },
  // 保存
  baocun:function(){
    var that = this;
    console.log(that.data.shopname)
    console.log(that.data.shopunit)
    var regNumber = /[A-Za-z]/; 
    if (that.data.shopname == ''){
      wx.showToast({
        title: '请填写名称'
      })
    }else if (that.data.one_class_quename == '' || that.data.two_class_quename == '') {
      wx.showToast({
        title: '请选择分类'
      }) 
    }else if (that.data.shopunit == '') {
      wx.showToast({
        title: '请选择单位'
      })
    } else if (that.data.dingjia == ''){
      wx.showToast({
        title: '请选择定价模式'
      })
    } else if (that.data.dingjia == '固定价' && that.data.shopmoney==''){
      wx.showToast({
        title: '请填写价格'
      })
    } else if (!that.data.str) {
      wx.showToast({
        title: '请选择拼音'
      })
    }else{
      var typestr = typeof this.data.str;
      if (typestr == "string") {
        var str = this.data.str;
      } else {
        var str = this.data.str.join('');
      }
      var str1 = ''
      for (var i in str) {
        if (regNumber.test(str[i])) {
          str1 += str[i];
        }
      }      
      app.AppHttp('api/Supplier/addGoodsDo', {
        openid: app.globalData.openid,
        cover: that.data.shopimg,
        trade_name: that.data.shopname.trim(),
        unit: that.data.unitindex,
        one_class: that.data.one_class,
        two_class: that.data.two_class,
        price_type: that.data.price_type,
        true_price: that.data.shopmoney,
        trade_alias: that.data.shopordername.trim(),
        specs: that.data.specs_deatail,
        abbreviation: str1.trim(),
        e_type:'client'
      }, function (res) {
          wx.showToast({
            title: res.data.msg
          });
          app.globalData.shopname = that.data.shopname;
          if (res.data.status==1) {
            app.globalData.shop_manage=1;
            if (that.data.xuni_status =='xuniadd'){
              wx.navigateBack({
                delta:1
              })
            }else{
              wx.navigateBack({
                delta: 2
              })
            }
          };
        }) 
      } 
  },
  // 输入名称
  shopnameinput:function(e){
    this.data.shopname = e.detail.value;
    this.makePy_fun();
  },
  makePy_fun:function(){
    app.makePy(this.data.shopname);
    if (app.globalData.pinyin.length > 1) {
      this.setData({
        strlist: app.globalData.pinyin
      })
    } else {
      this.setData({
        str: app.globalData.pinyin
      })
    }
  },
  // 多音字选组
  mySelectpinxie: function (e) {
    this.setData({
      str: e.target.dataset.me,
      strlist: ''
    })
  },
  //输入规格详情
  specsInput: function (e) {
    this.setData({
      specs_deatail: e.detail.value
    })
  },
  // 输入拼写
  pinxieinput: function (e) {
    console.log(e.detail.value);
    this.setData({
      str: e.detail.value
    })
  },
  // 输入备注
  ordernameinput:function(e){
    this.data.shopordername = e.detail.value;
  },
  // 选择单位弹窗
  setDanWei: function () {
    var that=this;
    util.showBuyModal(this);
    that.setData({
      selectdanwei: true
    });
    app.AppHttp('api/Supplier/unit_list', {
      
    }, function (res) {
      console.log(JSON.stringify(res.data))
      that.setData({
        unit_list: res.data
      })
    });
  },
  // 添加单位
  addunit:function(e){
    util.hideBuyModal(this,'selectdanwei');
    this.setData({
      shopunit: e.target.dataset.unitname,
      unitindex: e.target.dataset.index
    })
  },
  // 关闭单位弹窗
  closedanwei: function () {
    util.hideBuyModal(this,'selectdanwei');
  },
  // 点击所属分类弹窗
  fenleick: function () {
    util.showBuyModal(this);
    this.setData({
      showModalStatus: true
    })
    if (this.data.notwo==1){
      this.upshoplist();
    }
  },
  // 关闭所属分类弹窗
  closefenlei: function () {
    util.hideBuyModal(this,"showModalStatus");
  },
  // 确定并关闭所属分类弹窗
  fenleiconfirm: function () {
    if (this.data.notwo == 1){
      wx.showToast({
        title: '没有二级分类'
      })
      return;
    };
    util.hideBuyModal(this,"showModalStatus");
    this.setData({
      one_class_quename: this.data.one_class_name,
      two_class_quename: this.data.two_class_name,
      one_class: this.data.one_classzs,
      two_class: this.data.two_classzs
    })
  },
  // 选择所属分类
  bindChange: function (e) {
    var that = this;
    var val = e.detail.value;
    var one_class_list = this.data.one_class_list;
    var two_class_list = this.data.two_class_list;
    app.AppHttp('api/Supplier/CateList', {
      id: that.data.id,
      one_id: one_class_list[val[0]].private_one_class,
      e_type:'client'
    }, function (res) {
      // console.log(JSON.stringify(res.data));
      if (val[1] > res.data.two_class_list.length - 1) {
        val[1] = res.data.two_class_list.length - 1
      }
      if (res.data.two_class_list.length == 0) {
        that.setData({
          two_class_list: [''],
          notwo:1
        })
        return;
      }
      that.setData({
        two_class_list: res.data.two_class_list,
        one_classzs: res.data.one_class_list[val[0]].private_one_class,
        two_classzs: res.data.two_class_list[val[1]].private_one_class,
        one_class_name: res.data.one_class_list[val[0]].private_class_name,
        two_class_name: res.data.two_class_list[val[1]].private_class_name,
        value: val,
        notwo: 0
      });
    }) 
  },
  // 点击定价模式弹窗
  setdingjia: function () {
    this.setData({
      selectdingjia: false
    })
  },
  //点击定价模式切换
  mySelectdingjia: function (e) {
    this.setData({
      dingjia: e.target.dataset.me,
      selectdingjia: true
    })
    if(this.data.dingjia=='固定价'){
      this.setData({
        price_type:2
      })
    } else if (this.data.dingjia == '时价'){
      this.setData({
        price_type: 1
      })
    }
  },
  // 输入价格
  shopmoneyinput:function(e){
    this.data.shopmoney = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    app.globalData.pinyin=''; 
    console.log(options.xuni_status);
    this.setData({
      id: app.globalData.loginInfo.id,
      unit_height:0.64* wx.getSystemInfoSync().windowHeight,
      xuni_status: options.xuni_status,
      shopname:options.shopname
    });
    this.upshoplist();
  },
  upshoplist: function () {
    var that = this;
    app.AppHttp('api/Supplier/CateList', {
      id: that.data.id,
      e_type:'client'
    }, function (res) {
      // console.log(JSON.stringify(res.data))
      if (res.data.two_class_list.length == 0) {
        that.setData({
          one_class_list: res.data.one_class_list,
          one_class_name: res.data.one_class_list[0].private_class_name,
          one_classzs: res.data.one_class,
          two_class_list: [''],
          notwo:1
        })
        return;
      }
      that.setData({
        one_class_list: res.data.one_class_list,
        two_class_list: res.data.two_class_list,
        one_class_name: res.data.one_class_list[0].private_class_name,
        two_class_name: res.data.two_class_list[0].private_class_name,
        one_classzs: res.data.one_class,
        two_classzs: res.data.two_class,
        shopimg:that.data.shopimg,
        value:[0,0],
        notwo: 0
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    that.setData({
      id: app.globalData.loginInfo.id
    });
    if(app.globalData.imgSrc3!= '') {
      that.setData({
        imgSrc3: app.globalData.imgSrc3
      });
      console.log(app.globalData.imgSrc3)
      wx.uploadFile({
        url: app.globalData.url + 'api/Supplier/uploadGoodCover',
        filePath: that.data.imgSrc3,
        name: 'cover',
        method: "POST",
        success: function (res) {
          var obj = JSON.parse(res.data)
          console.log(obj)
          that.data.shopimg = obj.data
          // wx.showToast({
          //   title: '上传成功'
          // }, 2000);
          that.upshoplist();
          app.globalData.imgSrc3 = ''
        },
        fail: function (res) {
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
          })
          app.globalData.imgSrc3 = ''
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})